package com.wxb.service.impl;

import com.wxb.domain.MallCouponEntity;
import com.wxb.mapper.MallCouponMapper;
import com.wxb.service.IMallCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Date 2025-05-30 14:32
 * @Version 1.0
 * @Description
 * @ClassName MallCouponServiceImpl
 **/
@Service
public class MallCouponServiceImpl implements IMallCouponService {
    @Autowired
    private MallCouponMapper apiCouponMapper;

    public MallCouponEntity queryObject(Integer couponId) {
        return apiCouponMapper.queryObject(couponId);
    }

    public List<MallCouponEntity> queryList(Map<String, Object> map) {
        return apiCouponMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiCouponMapper.queryTotal(map);
    }


    public void save(MallCouponEntity userVo) {
        apiCouponMapper.save(userVo);
    }

    public void update(MallCouponEntity user) {
        apiCouponMapper.update(user);
    }

    public void delete(Long userId) {
        apiCouponMapper.delete(userId);
    }

    public void deleteBatch(Long[] userIds) {
        apiCouponMapper.deleteBatch(userIds);
    }

    public List<MallCouponEntity> queryUserCoupons(Map<String, Object> map) {
        // 检查优惠券是否过期
        List<MallCouponEntity> couponVos = apiCouponMapper.queryUserCoupons(map);
        for (MallCouponEntity couponVo : couponVos) {
            if (couponVo.getCouponStatus()==1) {
                // 检查是否过期
                if(couponVo.getUseEndDate().before(new Date())) {
                    couponVo.setCouponStatus(3);
                    apiCouponMapper.updateUserCoupon(couponVo);
                }
            }
            if (couponVo.getCouponStatus()==3) {
                // 检查是否不过期
                if(couponVo.getUseEndDate().after(new Date())) {
                    couponVo.setCouponStatus(1);
                    apiCouponMapper.updateUserCoupon(couponVo);
                }
            }
        }

        return couponVos;
    }

    public MallCouponEntity queryMaxUserEnableCoupon(Map<String, Object> map) {
        return apiCouponMapper.queryMaxUserEnableCoupon(map);
    }

    public List<MallCouponEntity> queryUserCouponList(Map<String, Object> map) {
        return apiCouponMapper.queryUserCouponList(map);
    }
}
